TIM0=Timer(Timer.Timer0)
TIM0_Flag=0
def Delay_ms(ms):
    global TIM0_Flag
    TIM0_Flag=0
    TIM0.start(period=ms, mode=Timer.ONE_SHOT, callback=TIM0_Callback)
    while TIM0_Flag==0:
        if TIM0_Flag==1:
            break
        pass
    TIM0_Flag=0

def TIM0_Callback(arg):
    global TIM0_Flag
    TIM0_Flag=1
    TIM0.stop()


#播放音乐

# -*- coding: utf-8 -*-
from misc import PWM_V2
from machine import Pin
import usr.Delay as Delay

C0 = 1635159
Db0 = 1732391
D0 = 1835404
Eb0 = 1944543
E0 = 2060172
F0 = 2182676
Gb0 = 2312465
G0 = 2449971
Ab0 = 2595654
A0 = 2750000
Bb0 = 2913523
B0 = 3086770

C1 = 3270319
Db1 = 3464782
D1 = 3670809
Eb1 = 3889087
E1 = 4120344
F1 = 4365352
Gb1 = 4624930
G1 = 4899942
Ab1 = 5191308
A1 = 5500000
Bb1 = 5827047
B1 = 6173541

C2 = 6540639
Db2 = 6929565
D2 = 7341619
Eb2 = 7778174
E2 = 8240688
F2 = 8730705
Gb2 = 9249860
G2 = 9799885
Ab2 = 10382617
A2 = 11000000
Bb2 = 11654094
B2 = 12347082

C3 = 13081278
Db3 = 13859131
D3 = 14683238
Eb3 = 15556349
E3 = 16481377
F3 = 17461411
Gb3 = 18499721
G3 = 19599771
Ab3 = 20765234
A3 = 22000000
Bb3 = 23308188
B3 = 24694165

C4 = 26162556
Db4 = 27718263
D4 = 29366476
Eb4 = 31112698
E4 = 32962755
F4 = 34922823
Gb4 = 36999442
G4 = 39199543
Ab4 = 41530469
A4 = 44000000
Bb4 = 46616376
B4 = 49388330

C5 = 52325113
Db5 = 55436526
D5 = 58732953
Eb5 = 62225396
E5 = 65925511
F5 = 69845646
Gb5 = 73998884
G5 = 78399087
Ab5 = 83060939
A5 = 88000000
Bb5 = 93232752
B5 = 98776660

C6 = 104650226
Db6 = 110873052
D6 = 117465907
Eb6 = 124450793
E6 = 131851022
F6 = 139691292
Gb6 = 147997769
G6 = 156798174
Ab6 = 166121879
A6 = 176000000
Bb6 = 186465504
B6 = 197553320

C7 = 209300452
Db7 = 221746104
D7 = 234931814
Eb7 = 248901586
E7 = 263702045
F7 = 279382585
Gb7 = 295995538
G7 = 313596348
Ab7 = 332243758
A7 = 352000000
Bb7 = 372931009
B7 = 395106641

C8 = 418600904
Db8 = 443492209
D8 = 469863628
Eb8 = 497803173
E8 = 527404091
F8 = 558765170
Gb8 = 591991076
G8 = 627192697
Ab8 = 664487516
A8 = 704000000
Bb8 = 745862018
B8 = 790213282

C9 = 837201808
Db9 = 886984419
D9 = 939727257
Eb9 = 995606347
E9 = 1054808182
F9 = 1117530340
Gb9 = 1183982152
G9 = 1254385395
Ab9 = 1328975032
A9 = 1408000000
Bb9 = 1491724036
B9 = 1580426564


def Pitch_Play(pwm,pitch,s):
    pitch=pitch/100000
    pwm.open(pitch, 50)
    Delay.Delay_ms(int(s*950))
    pwm.close()
    Delay.Delay_ms(int(s*50))

def Pitch_Play_HB_Start(GPIO,V2_PWM):
    Pin(GPIO, Pin.OUT,Pin.PULL_PD,0)
    pwm=PWM_V2(V2_PWM, G5/100000, 50)
    Pitch_Play(pwm,G5,0.25)
    Pitch_Play(pwm,G5,0.25)
    Pitch_Play(pwm,A5,0.5)
    Pitch_Play(pwm,G5,0.5)
    Pitch_Play(pwm,C6,0.5)
    Pitch_Play(pwm,B5,0.5)

def Pitch_Play_HB_End(GPIO,V2_PWM):
    Pin(GPIO, Pin.OUT,Pin.PULL_PD,0)
    pwm=PWM_V2(V2_PWM, G5/100000, 50)
    Pitch_Play(pwm,F5,0.25)
    Pitch_Play(pwm,F5,0.25)
    Pitch_Play(pwm,E6,0.5)
    Pitch_Play(pwm,C6,0.5)
    Pitch_Play(pwm,D6,0.5)
    Pitch_Play(pwm,C6,0.5)

